package com.dinadi.yidi.adapter;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dinadi.yidi.R;
import com.dinadi.yidi.bean.AddressManagementEntity;
import com.dinadi.yidi.ui.activity.DeliveryAddressActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * author: JiaYu.He
 * Date: 2017/7/14
 * Time: 10:49
 */
public class AddressManagementAdapter extends BaseAdapter {

    private Activity mContext;
    List<AddressManagementEntity> mDatas = new ArrayList<>();

    public AddressManagementAdapter(Activity context, List<AddressManagementEntity> mList) {
        this.mContext = context;
        this.mDatas = mList;
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.adapter_address_management_item, null); /**这个是评论的item列表*/
            viewHolder = new ViewHolder();
            viewHolder.tv_goods_receipt = (TextView) convertView.findViewById(R.id.tv_goods_receipt);
            viewHolder.tv_goods_receipt_phone = (TextView) convertView.findViewById(R.id.tv_goods_receipt_phone);
            viewHolder.tv_place = (TextView) convertView.findViewById(R.id.tv_place);
            viewHolder.img_address_delete = (ImageView) convertView.findViewById(R.id.img_address_delete);
            viewHolder.layout_order_details = (LinearLayout) convertView.findViewById(R.id.layout_order_details);
            viewHolder.rb_address_default = (ImageView) convertView.findViewById(R.id.rb_address_default);
            viewHolder.edit_address = (ImageView) convertView.findViewById(R.id.edit_address);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.tv_goods_receipt.setText(mDatas.get(position).getRecipients());
        viewHolder.tv_goods_receipt_phone.setText(mDatas.get(position).getPhone());
        viewHolder.tv_place.setText(mDatas.get(position).getAddressArea() + mDatas.get(position).getAddressDetail());
        String isDefault = mDatas.get(position).getIsDefault();


                /* "isDefault": "是否默认 1-正常 2-默认",*/
        if (isDefault.equals("2")) {
            viewHolder.rb_address_default.setSelected(true);
        } else {
            viewHolder.rb_address_default.setSelected(false);

        }
        viewHolder.rb_address_default.setOnClickListener(new AddressManagementAdapterOnClickListener(position));
        viewHolder.img_address_delete.setOnClickListener(new AddressManagementAdapterOnClickListener(position));
        viewHolder.layout_order_details.setOnClickListener(new AddressManagementAdapterOnClickListener(position));

        /*修改地址*/
        viewHolder.edit_address.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(mContext, DeliveryAddressActivity.class);
                intent.putExtra("addressId", mDatas.get(position).getAddressId());
                intent.putExtra("isModify", "1");
                mContext.startActivity(intent);

            }
        });

        return convertView;
    }


    class ViewHolder {
        TextView tv_goods_receipt, tv_goods_receipt_phone, tv_place;
        ImageView img_address_delete, edit_address;
        LinearLayout layout_order_details;
        ImageView rb_address_default;
    }

    private class AddressManagementAdapterOnClickListener implements View.OnClickListener {
        int position;

        public AddressManagementAdapterOnClickListener(int position) {
            this.position = position;
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.rb_address_default:

                    /**在这里写个接口，*/
                    onClickListenerCallBack.onClickDefault(position, mDatas.get(position).getAddressId());

                    break;
                case R.id.img_address_delete:
                    onClickListenerCallBack.onClickDelete(position, mDatas.get(position).getAddressId());
                    break;
                case R.id.layout_order_details:
                    onClickListenerCallBack.onClickInto(position, mDatas.get(position).getAddressId());
                    break;
            }
        }
    }

    public interface OnClickListenerCallBack {
        void onClickDefault(int position, String id);

        void onClickDelete(int position, String id);

        void onClickInto(int position, String id);
    }

    public OnClickListenerCallBack onClickListenerCallBack;

    public void setOnClickListenerCallBack(OnClickListenerCallBack onClickListenerCallBack) {
        this.onClickListenerCallBack = onClickListenerCallBack;
    }
}

